function preloadPictures(arrayOfImages, wrapper) {
    if (arrayOfImages.length <= 0) {
        slideshowStart();
    } else {
        var img = $('<img />').attr({src: arrayOfImages[0], alt: 'obrazek'}).css('display', 'none');
        img.load(function() {
            wrapper.append(img);
            arrayOfImages.shift();
            preloadPictures(arrayOfImages, wrapper);
        });
    }
}

$(function() {
    var slideShowPictures = new Array(
            "/images/slides/slide1.jpg",
            "/images/slides/slide2.jpg",
            "/images/slides/slide3.jpg",
            "/images/slides/slide4.jpg"
            );

    preloadPictures(slideShowPictures, $('#slideshow-pics'));
});

var slideIndex = 0;
var maxSlideIndex = 0;

function slideshow() {
    var current = $('#slideshow-pics img').eq(slideIndex);

    current.css('z-index', 20);
    current.delay(5000).fadeOut(1000, slideshow);

    if (slideIndex < maxSlideIndex) {
        slideIndex++;
    } else {
        slideIndex = 0;
    }

    var next = $('#slideshow-pics img').eq(slideIndex);
    next.css('z-index', 10).show();
}

function slideshowStart() {
    numOfSlides = $('#slideshow-pics img').length;
    maxSlideIndex = numOfSlides - 1;
    $('#slideshow-frame').css('backgroundImage', 'none');

    if (numOfSlides < 1) {
        return;
    } else if (numOfSlides == 1) {
        $('#slideshow-pics img').eq(0).show();
    } else {
        $('#slideshow-pics img').eq(0).show();
        slideshow();
    }
}

